﻿

namespace SignalRHub.Redis.Interface
{
    /// <summary>
    /// 缓存管理接口
    /// </summary>
    public interface ICacheManager
    {
        /// <summary>
        /// 通过制定的Key获取缓存的数据，不存在则返回默认值
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">key</param>
        /// <returns>缓存的数据</returns>
        T Get<T>(string key);

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="data">数据</param>
        /// <param name="cacheTime">缓存时间，分钟数</param>
        void Set(string key, object data, int cacheTime);

        public void SetAsync(string key, object data, int cacheTime);

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="data">数据</param>
        /// <param name="cacheTime">缓存时间，分钟数</param>
        bool SetValue<T>(string key, T data, int cacheTime);

        public Task<bool> SetValueAsync<T>(string key, T data, int cacheTime);

        /// <summary>
        /// 验证缓存是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        bool IsSet(string key);

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key"></param>
        void Remove(string key);

        /// <summary>
        /// 正则移除缓存
        /// </summary>
        /// <param name="pattern"></param>
        void RemoveByPattern(string pattern);

        /// <summary>
        /// 清理缓存
        /// </summary>
        void Clear();

        /// <summary>
        /// 自增
        /// <param name="key"></param> 
        /// <param name="value">自增的值</param> 
        /// </summary>
        long StringIncrement(string key, long value);

        /// <summary>
        /// 插入List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void ListSet<T>(string key, List<T> value);
        public Task ListSetAsync<T>(string key, List<T> value);

        /// <summary>
        /// 获取List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<T> ListGet<T>(string key);

        /// <summary>
        /// 移除List对应的某一列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public long LisRemove<T>(string key, T value);
        public Task<long> LisRemoveAsync<T>(string key, T value);

        /// <summary>
        /// 加锁执行方法（无返回值）
        /// </summary>
        /// <param name="lockKey"></param>
        /// <param name="act"></param>
        /// <param name="expiry"></param>
        /// <param name="waitTime"></param>
        bool ExecuteWithLock(string lockKey, Action act, TimeSpan? expiry = null, TimeSpan? waitTime = null);
    }
}
